package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.constant.Constants;
import com.example.demo.model.Result;
import com.example.demo.model.domain.User;
import com.example.demo.service.UserService;
import com.example.demo.mapper.UserMapper;
import com.example.demo.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Lenovo
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2025-03-14 16:04:49
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public Result<User> userRegister(String userAccount, String password, String checkPassword) {
        //1.校验
        if (!StringUtils.hasLength(userAccount)||
            !StringUtils.hasLength(password)||
            !StringUtils.hasLength(checkPassword)){
            return Result.fail("账号或者密码不能为空");
        }
        if (userAccount.length()<4){
            return Result.fail("用户名长度不能小于4位");
        }
        if (password.length()<8||checkPassword.length()<8){
            return Result.fail("密码长度不能小于8位");
        }
        if (!password.equals(checkPassword)){
            return Result.fail("两次密码不一致");
        }
        //2.账号不能重复
        QueryWrapper<User> queryWrapper=new QueryWrapper<User>();
        queryWrapper.eq("userAccount",userAccount);
        long count = this.count(queryWrapper);
        if (count!=0){
            return Result.fail("用户名存在,请重新输入");
        }
        String pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$";
        Matcher matcher = Pattern.compile(pattern).matcher(userAccount);
        if (!matcher.find()){
            return Result.fail("用户名必须由字母和数字组成");
        }
        //3.密码加密
        
        String encrypt = SecurityUtils.encrypt(password);
        //4.插入数据
        User user=new User();
        user.setUserAccount(userAccount);
        user.setPassword(encrypt);
        boolean save = this.save(user);
        if (!save){
            return Result.fail("内部错误");
        }
        user.setPassword("");
        return Result.success(user);
    }

    @Override
    public Result<User> doLogin(String userAccount, String password, HttpServletRequest request) {
        //1.校验
        if (!StringUtils.hasLength(userAccount)||
                !StringUtils.hasLength(password)||
                !StringUtils.hasLength(password)){
            return Result.fail("账号或者密码不能为空");
        }
        if (userAccount.length()<4){
            return Result.fail("用户名长度不能小于4位");
        }
        if (password.length()<8){
            return Result.fail("密码长度不能小于8位");
        }
        //账号必须由字母数字组成
        String pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$";
        Matcher matcher = Pattern.compile(pattern).matcher(userAccount);
        if (!matcher.find()){
            return Result.fail("用户名必须由字母和数字组成");
        }

        //2.用户是否已经注册
        QueryWrapper<User> queryWrapper=new QueryWrapper<User>();
        queryWrapper.eq("userAccount",userAccount);
        long count = this.count(queryWrapper);
        if (count==0){
            return Result.fail("用户不存在,请重新输入");
        }

        //3.校验输入的密码是否和数据库中的一致
        User user = userMapper.SelectByName(userAccount);
        boolean verify = SecurityUtils.verify(password, user.getPassword());
        if (verify==false){
            return Result.fail("密码错误,请重新输入");
        }


        User safetyUser=new User();
        safetyUser.setId(user.getId());
        safetyUser.setUsername(user.getUsername());
        safetyUser.setAvatarUrl("");
        safetyUser.setGender(-1);
        safetyUser.setUserAccount(user.getUserAccount());
        safetyUser.setPhone("");
        safetyUser.setEmail("");
        //设置session,看用户是否登录
        request.getSession().setAttribute(Constants.USER_LONGIN_KEY,safetyUser);

        return Result.success(safetyUser);
    }
}




